package boot

import (
	"gitee.com/johng/gf/g"
	"gitee.com/johng/gf/g/frame/gins"
	"gitee.com/johng/gf/g/net/ghttp"
)

// 用于配置初始化.
func init() {
	cfg := g.Config()
	view := gins.View()
	server := g.Server()
	cfg.AddPath("config")
	view.AddPath("static/")
	view.SetDelimiters("${", "}")
	server.AddSearchPath("/static/")

	//server.SetDumpRouteMap(false)
	server.EnableAdmin(cfg.GetString("admin-partten"))
	server.SetPort(cfg.GetInt("http-port"))
	router(server)
}

//初始路由注册
func router(server *ghttp.Server) {

	server.SetDenyRoutes([]string{
		"/config/*",
	})
	server.BindHookHandler("/home/*action", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {

		if r.Get("action") != "login" {
			if r.Session.Get("admin") == nil {
				r.Response.WriteTpl("login.html", map[string]interface{}{})
				r.Exit()
				if !r.IsExited() {
					r.Response.Write("请重新登录！")
				}
			} else {

			}
		}
	})
	server.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, func(r *ghttp.Request) {
		r.Response.RedirectTo("/home")
	})

	server.BindStatusHandler(404, func(r *ghttp.Request) {
		r.Response.WriteTpl("404.html", map[string]interface{}{})
	})
}
